<template>
    <header class="schedule-calendar-hd">
        <button type="button" class="schedule-calendar-arrow double-arrow" @click="prevYear">&lt;&lt;</button>
        <button type="button" class="schedule-calendar-arrow" @click="prevMonth">&lt;</button>
        <div class="schedule-calendar-picker" ref="picker">
            <div role="button"
                 class="schedule-calendar-display"
                 @click="pickerVisible = !pickerVisible">
                 <span>{{month + 1}}月</span> 
                <i class="el-icon-arrow-down"></i>
                 <span>{{year}}年</span>
            </div>
            <picker :visible="pickerVisible":year="year" :month="month"></picker>
        </div>
        <button type="button" class="schedule-calendar-arrow" @click="nextMonth">&gt;</button>
        <button type="button" class="schedule-calendar-arrow double-arrow" @click="nextYear">&gt;&gt;</button>
    </header>
</template>


<script>

    import { calcPrevMonth, calcNextMonth } from './utils'
    import picker from './picker'

    export default {
        components: {
            picker
        },
        props: {
            year: Number,
            month: Number
        },
        data() {
            return {
                pickerVisible: false
            }
        },
        computed: {

        },
        methods: {
            updateValue(year, month = this.month) {
                this.$emit('updateValue', year, month)
            },
            prevYear() {
                this.updateValue(this.year - 1)
            },
            nextYear() {
                this.updateValue(this.year + 1)
            },
            prevMonth() {
                const { year, month } = calcPrevMonth(this.year, this.month)
                this.updateValue(year, month)
            },
            nextMonth() {
                const { year, month } = calcNextMonth(this.year, this.month)
                this.updateValue(year, month)
            },
            clickOutSide(e) {
                if (this.pickerVisible && !this.$refs.picker.contains(e.target)) {
                    this.pickerVisible = false
                }
            }
        },
        created() {
            document.addEventListener('mouseup', this.clickOutSide)
        },
        destoryed() {
            document.removeEventListener('mouseup', this.clickOutSide)
        }
    }
</script>


<style lang="scss">

    .schedule-calendar-hd {
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: left;
        justify-content: left;
        -ms-flex-line-pack: left;
        align-content: left;
        -ms-flex-align: left;
        align-items: left;
        padding: 0 0 40px;
        font-size: 18px;
        line-height: 1;
        // background: #2196f3;
        color: #fff;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;

        .schedule-calendar-picker .schedule-calendar-display {
            padding: 0;
        }

        >button {
            color: #2ab4e6;
            border: 0;
            outline: none;
            cursor: pointer;
            border-radius: 2px;
            background: transparent;
            transition: 0.2s ease-in-out;
            opacity: 0;
            visibility: hidden;
            transition: .3s;
            display: none;
        }

        &:hover {
            >button {
                opacity: 1;
                visibility: visible;
            }
        }
    }

    .schedule-calendar-arrow {
        font-family: consolas;
        font-size: inherit;
        font-weight: 400;
        padding: 0 10px;
        height: 100%;
        color: #bbdefb;
    }

    .schedule-calendar-picker {
        position: relative;
        z-index: 20;
        padding: 4px 5px;
        height: 100%;

        .schedule-calendar-display {
            padding: 0 10px;
            line-height: 32px;
            cursor: pointer;
            position: relative;
            >span {
                &:nth-of-type(1) {
                    color: #2ab4e6;
                    font-size: 26px;
                    font-weight: bold;
                    margin-right: 5px;
                }

                &:nth-of-type(2) {
                    color: #cedff4;
                    margin-left: 30px;
                }
            }
            >i{
               color: #2ab4e6; 
              //  position: absolute;
              // bottom: 3px;
              // left: 50px;
              font-size: 16px;
            }
        }
    }
</style>